package com.kuaimacode.kframework.admin.controller.user;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.user.UserDrawApplyService;
import com.kuaimacode.kframework.admin.vo.response.UserWithdrawalVO;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.controller.user
 * @Description: created by IntelliJ IDEA
 * @date 2019-05-25 09:30
 */
@Controller
@RequestMapping({"/userdraw"})
public class DrawApplyController {
    private static String PAGE_FILE_NAME = "userWithdrawal/";

    @Autowired
    private UserDrawApplyService userDrawApplyService;
    @Autowired
    private DictService dictService;

    @RequestMapping(value = {"/index"})
    public String index(
            @RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
            @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
            UserWithdrawalVO searchVO, ModelMap model) {
        PageInfo<UserWithdrawalVO> pageInfo = userDrawApplyService.findNormalUserDrawApply(pageNum, pageSize, searchVO);
        // pageInfo.list order
        model.put("pageInfo", pageInfo);
        model.put("searchVO", searchVO);
        return PAGE_FILE_NAME + "index";
    }

    @PostMapping("approval/{id}")
    @ResponseBody
    public JsonResult approval(@PathVariable Long id, UserWithdrawalVO withdrawalVO) {
        if (!dictService.getChildren("WITHDRAWAL_STATUS")
                .stream()
                .map(Dict::getCode)
                .collect(Collectors.toList())
                .contains(withdrawalVO.getApplyStatus())) {
            throw new ServerInternalErrorException(ErrorEnum.PARAM_VALID_ERROR);
        }

        userDrawApplyService.approval(id, withdrawalVO);
        return JsonResult.success();
    }

    @RequestMapping(value = {"detail/{id}"})
    public String detail(@PathVariable Long id, ModelMap model) {
        model.put("item", userDrawApplyService.findSingleNormalUserDrawApply(id));
        return PAGE_FILE_NAME + "detail";
    }
}
